import 'package:changxindianzishangwu/dao/title_dao.dart';
import 'package:changxindianzishangwu/model/titles_list.dart';
import 'package:changxindianzishangwu/util/colors.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

class SetTitlePage extends StatefulWidget {
  String userId;
  SetTitlePage({Key key, this.userId}) : super(key: key);

  @override
  _SetTitlePageState createState() => _SetTitlePageState();
}

class _SetTitlePageState extends State<SetTitlePage> {
  TextEditingController textEditingController = TextEditingController();
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    selectTitle();
  }

  Color defaultColor = ZdColor.rgba(153, 153, 153, 1);
  Color actionColor = ZdColor.rgba(0, 220, 169, 1);
  TitleList titleList;
  List<Widget> itemWidget = [];
  int type;
  List<String> a = [];
  selectTitle() async {
    itemWidget = [];
    titleList = await TitleDao.selectTitle();

    if (titleList.resultCode == 1) {
      titleList.data.forEach((e) {
        a.add(e.groupName);
        itemWidget.add(InkWell(
          onTap: () {
            setState(() {
              titleList.currentTime = 1;
            });
          },
          child: Container(
            alignment: Alignment.center,
            margin: EdgeInsets.all(5.w),
            width: 50.w,
            height: 30,
            decoration: BoxDecoration(
                border: Border.all(
                  color:
                      titleList.currentTime != 1 ? defaultColor : actionColor,
                ),
                borderRadius: BorderRadius.circular(12)),
            child: Text(
              e.groupName,
              style: TextStyle(fontSize: 12.sp),
              maxLines: 1,
            ),
          ),
        ));

        e.userIdList.forEach((element) {
          print("${widget.userId}-----------------${element}");
          if (element == widget.userId) {
            textEditingController.text = e.groupName;
            return type = 1;
          }
        });
        textEditingController.text = a.toString();
      });
    }
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        elevation: 0,
        title: Text(
          "设置备注",
          style: TextStyle(
            fontSize: ScreenUtil().setSp(16),
          ),
        ),
        centerTitle: true,
        actions: [
          InkWell(
            onTap: () async {
              titleList.data.forEach((element) {
                if (textEditingController.text.trim() != "" &&
                    textEditingController.text != '') {
                  if (element.groupName == textEditingController.text) {
                    element.userIdList.remove(widget.userId);
                    element.userIdList.add(widget.userId);
                    TitleDao.updateTitle(element.groupId, element.userIdList);
                    Get.back();
                  } else {
                    print("11==============================================");
                  }
                }
              });
            },
            child: Container(
              alignment: Alignment.center,
              margin: EdgeInsets.only(top: 11, bottom: 11, right: 15),
              height: 10,
              width: ScreenUtil().setWidth(60),
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(6),
                color: Color.fromRGBO(0, 220, 169, 1),
              ),
              child: Text(
                "完成",
                style: TextStyle(
                    color: Colors.white, fontSize: ScreenUtil().setSp(14)),
              ),
            ),
          )
        ],
      ),
      body: Container(
        child: Column(
          children: [
            Container(
              color: Colors.white,
              alignment: Alignment.centerLeft,
              padding: EdgeInsets.only(left: 15.w),
              child: Text(
                "输入标签",
                style: TextStyle(
                    fontSize: 12.sp, color: ZdColor.rgba(193, 193, 193, 1)),
              ),
            ),
            Container(
              color: Colors.white,
              height: 60,
              alignment: Alignment.centerLeft,
              padding: EdgeInsets.only(left: 15.w),
              child: TextField(
                controller: textEditingController,
                onChanged: (value) {
                  print(value);
                },
                keyboardType: TextInputType.name,
                style: TextStyle(
                    fontSize: 15.sp, color: ZdColor.rgba(0, 220, 169, 1)),
                cursorColor: Colors.greenAccent,
                decoration: InputDecoration(
                  hintText: "请输入",
                  border: InputBorder.none,
                ),
              ),
            ),
            SizedBox(
              height: 10,
            ),
            Container(
              alignment: Alignment.centerLeft,
              padding: EdgeInsets.only(left: 15.w, top: 6),
              child: Text(
                "所有标签",
                style: TextStyle(
                    fontSize: 12.sp, color: ZdColor.rgba(193, 193, 193, 1)),
              ),
            ),
            Wrap(
              children: itemWidget,
            )
          ],
        ),
      ),
    );
  }
}
